#include "objectiveform.h"
#include "ui_objectiveform.h"
#include <QPainter>
#include <QStyleOption>
#include "objectivechooseform.h"

ObjectiveForm::ObjectiveForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ObjectiveForm),
    m_associatedForm(nullptr)
{
    ui->setupUi(this);
    this->setProperty("form","backgroundform");
    ui->leftBorderForm->SetLabelIcon("objective");
    this->setFixedHeight(80);
}

ObjectiveForm::~ObjectiveForm()
{
    delete ui;
}

void ObjectiveForm::UpdateShowType(int type)
{
    ui->leftBorderForm->UpdateShowType(type);
}

void ObjectiveForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void ObjectiveForm::on_comboBox_currentIndexChanged(const QString &arg1)
{
    ui->label_text->setText(arg1);
    
    // 从选中的内容中提取倍数信息
    int magnification = 10; // 默认值
    QString text = arg1;
    
    if (text.contains("100X", Qt::CaseInsensitive)) {
        magnification = 10;
    } else if (text.contains("50X", Qt::CaseInsensitive)) {
        magnification = 50;
    } else if (text.contains("20X", Qt::CaseInsensitive)) {
        magnification = 20;
    } else if (text.contains("10X", Qt::CaseInsensitive)) {
        magnification = 10;
    }else if (text.contains("5X", Qt::CaseInsensitive)) {
        magnification = 5;
    }
    
    // 发出信号，通知倍数变化
    emit objectiveMagnificationChanged(magnification);
    
    // 如果有关联的ObjectiveChooseForm，同步更新其选择
    if (m_associatedForm) {
        m_associatedForm->setChooseMode(arg1);
    }
}

void ObjectiveForm::setComboBoxValue(const QString &text)
{
    // 临时断开信号连接，避免无限循环
    QObject::disconnect(ui->comboBox, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this, &ObjectiveForm::on_comboBox_currentIndexChanged);
    
    // 查找并设置匹配的文本，使用宽松匹配方式
    int index = ui->comboBox->findText(text, Qt::MatchContains);
    if (index >= 0) {
        ui->comboBox->setCurrentIndex(index);
    }
    
    // 重新连接信号
    QObject::connect(ui->comboBox, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this, &ObjectiveForm::on_comboBox_currentIndexChanged);
}

void ObjectiveForm::setAssociatedObjectiveChooseForm(ObjectiveChooseForm *form)
{
    // 如果已经有关联的表单，先断开连接
    if (m_associatedForm) {
        QObject::disconnect(m_associatedForm, &ObjectiveChooseForm::chooseModeChanged, this, &ObjectiveForm::setComboBoxValue);
    }
    
    // 设置新的关联表单
    m_associatedForm = form;
    
    // 如果新表单不为空，建立连接
    if (m_associatedForm) {
        QObject::connect(m_associatedForm, &ObjectiveChooseForm::chooseModeChanged, this, &ObjectiveForm::setComboBoxValue);
        
        // 初始同步，确保两个控件显示相同的值
        setComboBoxValue(m_associatedForm->getCurrentText());
    }
}

void ObjectiveForm::on_toolButton_contractExpand_clicked(bool checked)
{
    ui->widget_hide->setHidden(!checked);
    if(checked)
        this->setFixedHeight(80);
    else
        this->setFixedHeight(50);
}
